﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using Entities;
using BUS;
using Utility;

public partial class A_LetsDeal_DoanhNghiep_dangsanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username_dn"] != null)
            {
                load_data();
            }
            else
            {
                Response.Redirect("~/A_LetsDeal-DoanhNghiep/Default.aspx");
            }
        }
    }

    protected void load_data()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        Tbl_SanPham_Entities sp = Tbl_SanPham_BUS.GetByID(id);
        txt_masp.Text = sp.MaSP.ToString();
        ftb_tensp.Text = sp.TenSP;
        txt_giaban.Text = sp.GiaBan.ToString();
        txt_giagoc.Text = sp.GiaGoc.ToString();
        ftxt_thongtincoban.Text = sp.ThongTinCoBan;
        ftxt_thongtindaydu.Text = sp.ThongTinDayDu;
        FreeTextBox1.Text = sp.DKSD;
        txt_slb.Text = sp.SLBan.ToString();
        DropDownList1.SelectedValue = sp.TinhTrang;
        if (sp.NgayKetThucDuKien != "")
        {
            DateTime dtin = DateTime.Parse(sp.NgayKetThucDuKien);
            DateTime dtout = DateTime.Parse(dtin.Day + "/" + dtin.Month + "/" + dtin.Year);
            Calendar1.SelectedDate = dtout;
            TextBox1.Text = dtout.ToShortDateString();
        }
        ftb_tgsd.Text = sp.ThoiGianSuDung;
        HiddenField1.Value = sp.DoanhNghiep.ToString();
        HiddenField2.Value = sp.LoaiSP.ToString();
        txt_ngaydang.Text = sp.NgayDang;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
    }
    
    protected void bt_tieptuc_Click(object sender, EventArgs e)
    {
        try
        {
            Tbl_SanPham_Entities sp = new Tbl_SanPham_Entities();
            sp.MaSP = txt_masp.Text;
            sp.TenSP = ftb_tensp.Text;
            sp.GiaGoc = float.Parse(txt_giagoc.Text);
            sp.GiaBan = float.Parse(txt_giaban.Text);
            sp.ThongTinCoBan = ftxt_thongtincoban.Text;
            sp.ThongTinDayDu = ftxt_thongtindaydu.Text;
            sp.DKSD = FreeTextBox1.Text;
            sp.SLBan = int.Parse(txt_slb.Text);
            sp.TinhTrang = DropDownList1.SelectedValue;
            sp.NgayKetThuc = Calendar1.SelectedDate.ToShortDateString();
            float giagoc = float.Parse(txt_giagoc.Text);
            float giaban = float.Parse(txt_giaban.Text);
            float tyle = float.Parse(((100 - giaban * 100 / giagoc) / 100).ToString());
            sp.TyLeGiamGia = round_float(tyle);
            sp.STT = int.Parse(Request.QueryString["id"].ToString());
            sp.NgayBan = DateTime.Now.ToString();
            if (DropDownList1.SelectedValue == "Close")
            {
                sp.NgayKetThuc = DateTime.Now.ToString();
                sp.NgayKetThucDuKien = Calendar1.SelectedDate.ToShortDateString();
            }
            else
            {
                sp.NgayKetThuc = "";
                sp.NgayKetThucDuKien = Calendar1.SelectedDate.ToShortDateString();
            }
            sp.ThoiGianSuDung = ftb_tgsd.Text;
            sp.DoanhNghiep = int.Parse(HiddenField1.Value);
            sp.LoaiSP = int.Parse(HiddenField2.Value);
            sp.NgayDang = txt_ngaydang.Text;
            Tbl_SanPham_BUS.Update2(sp);
            hyp_thongbao.Visible = true;
            hyp_thongbao.Text = "Cập nhật thông tin sản phẩm thành công!";
            hyp_thongbao.NavigateUrl = "~/A_LetsDeal-DoanhNghiep/sanpham.aspx";
        }
        catch
        {
            hyp_thongbao.Visible = true;
            hyp_thongbao.Text = "Đã xảy ra lỗi!";
            hyp_thongbao.NavigateUrl="#";
        }
    }

    protected float round_float(float n)
    {
        int t, u;
        t = Convert.ToInt32(n * 1000);
        u = Convert.ToInt32(t % 10);

        if (u >= 5)
        {
            u = t / 10 + 1;
        }
        else
            u = t / 10;
        n = (float)u / 100;

        return n;


    }
}
